var adminBlog={
    laypage:null,
    layer:null,
    page:function (url) {
        var total = $("#total").val();
        var curr = $("#curr").val();
        var keyWords = $("#keywords").val();
        layui.use(['laypage','layer'],function () {
            adminBlog.laypage = layui.laypage;
                adminBlog.layer = layui.layer;
            this.laypage.render({
                elem:'page',
                count:total,
                curr:curr,
                limit:10,
                jump:function (obj,first) {
                    if(!first){
                        if (keyWords == "" || keyWords==undefined){
                            window.location.href=url+"?pageNum="+obj.curr+"&pageSize="+obj.limit;
                        } else{
                            window.location.href=url+"?pageNum="+obj.curr+"&pageSize="+obj.limit+"keyWords="+keyWords;
                        }
                    }
                }
            });
        });
    },
    openCongitm:function (info,obj,url) {
        this.layer.confirm('确定要'+info+"?",{
            btn: [info,'取消']},
            function () {
             var value=$(obj).parents('tr').children('td').eq(1).text();
             $.ajax({
                 type:"post",
                 url:url,
                 dataType:'json',
                 async:false,
                 data:{param:value},
                 success:function (data) {
                     if(data.code == 200){
                         layer.msg(data.msg,{time:500,icon:1});
                         location.href="/article/list";
                         if (info == "删除"){
                             $(obj).parents('tr').remove();
                         }
                     }else if(data.code==101){
                         layer.msg(data.msg,{time:500,icon:2});
                     }
                 }
             })
            },
            function () {
                layer.closeAll();
            }
            );
    },
    edit:function (obj,url) {
        var value = $(obj).parents('tr').children('td').eq(1).text();
        location.href=url+"?artId="+value;
    },
    serach:function (url,id) {
        var keyWords=$("#"+id).val();
        window.location.href=url+"?keyWords="+keyWords;
    },
    addView:function (url) {
        location.href=url;
    },
    open:function (id,info) {
        this.layer.open({
            type:1,
            title:info,
            area:['400px','200px'],
            content:$("#"+id)
        });
    },
    add:function (formId,url,flag) {
           $.ajax({
               type:'post',
               url:url,
               dataType:'json',
               async:false,
               data:$("#"+formId).serialize(),
               success:function (result) {
                   if (result.code == 200){
                        var tbody = document.getElementById('labelTbody');
                        var trow = adminBlog.setTableRow(result.data,flag);
                        tbody.appendChild(trow);
                        adminBlog.layer_close();
                   }else{
                       adminBlog.layer.msg(result.msg,{time:500,icon:2});
                   }
               }
        });
    },
    del:function(obj,flag){
        var value = $(obj).parents('tr').children('td').eq(1).text();
        var url="";
        if (flag==1){
            url="/lable/del";
        } else if(flag == 2){
            url = "/type/del";
        }else if(flag == 3){
            url = "/link/del";
        }
        if (url !=""){
            $.ajax({
                type:'post',
                url:url,
                dataType:'json',
                data:{id:value},
                success:function (result) {
                    if (result.code ==200){
                        adminBlog.layer.msg(result.msg,{time:500,icon:1});
                        $(obj).parents('tr').remove();
                    }
                }
            });
        }

    },
    setTableRow:function (data,flag) {
        var row = document.createElement('tr');
        row.setAttribute("className",'text-c');
        var checkCell = document.createElement('td');
        checkCell.innerHTML="<input type='checkbox' value='"+data.id+"'/>";
        checkCell.style.textAlign='center'
        row.appendChild(checkCell);
        var idCell = document.createElement('td');
        idCell.innerHTML=data.id;
        idCell.style.textAlign='center';
        row.appendChild(idCell);
        var labelNameCell = document.createElement('td');
        if (flag ==1){
            labelNameCell.innerHTML=data.lableName;
        }
        if (flag ==2){
            labelNameCell.innerHTML=data.typeName;
        }
        if (flag == 3){
            labelNameCell.innerHTML=data.title;
        }
        labelNameCell.style.textAlign='center';
        if (flag == 3){
            var urlPathCell = document.createElement('td');
            urlPathCell.innerHTML = data.urlPath;
            row.appendChild(urlPathCell);
        }
        row.appendChild(labelNameCell);
        var oprotCell = document.createElement('td');
        oprotCell.innerHTML="<a style='text-decoration:none' class='ml-5' onClick='adminBlog.del(this,1)' href='javascript:;' title='删除'><i class='Hui-iconfont'>&#xe6e2;</i></a>";
        oprotCell.style.textAlign='center';
        row.appendChild(oprotCell);
        return row;
    },
    layer_close:function () {
        this.layer.closeAll();
    }
}